package org.hiucung.orangemusic.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.hiucung.orangemusic.bean.MusicBean;
import org.hiucung.orangemusic.R;

import java.util.List;

/***
 * 为RecyclerView编写Adapter适配器
 */
public class MusicAdapter extends RecyclerView.Adapter<MusicAdapter.MusicViewHolder> {

    //记录上次选择位置
    private static int LAST_CHOICE = -1;
    //记录当前选择位置
    private static int NOW_CHOICE = -1;

    Context context;//上下文对象
    List<MusicBean> mDatas;//媒体数据源

    OnItemClickListener onItemClickListener;//item监听器

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    //通过接口回调的方式
    public interface OnItemClickListener{
        public void OnItemClick(View view,int position);
    }

    public MusicAdapter() {

    }

    public MusicAdapter(Context context, List<MusicBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    public MusicViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_local_music,parent,false);
        MusicViewHolder holder = new MusicViewHolder(view);
        return holder;
    }

    /***
     * 适配器从mdata数据中一一匹配到ui中
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(@NonNull MusicViewHolder holder, int position) {
        MusicBean musicBean = mDatas.get(position);
        holder.idTv.setText(musicBean.getId());
        holder.songTv.setText(musicBean.getSong());
        holder.singerTv.setText(musicBean.getSinger());
        holder.timeTv.setText(musicBean.getDuration());
        holder.albumTv.setText(musicBean.getAlbum());

        //设置选中样式
        if (NOW_CHOICE == position) {
            holder.idTv.setVisibility(View.INVISIBLE);
            holder.audioview.setVisibility(View.VISIBLE);
        } else {
            holder.idTv.setVisibility(View.VISIBLE);
            holder.audioview.setVisibility(View.INVISIBLE);
        }

        //可以通过itemiew获取每一项的歌曲
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.OnItemClick(v,position);
                //点击时设置选中样式
                if (LAST_CHOICE != -1) {
                    notifyItemChanged(LAST_CHOICE);
                }
                holder.idTv.setVisibility(View.INVISIBLE);//歌曲id设置不可见
                holder.audioview.setVisibility(View.VISIBLE);//audioview的波浪动画设置可见
                LAST_CHOICE = position;
                NOW_CHOICE = position;
                notifyDataSetChanged();
            }
        });

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    static class MusicViewHolder extends RecyclerView.ViewHolder{

        TextView idTv,singerTv,albumTv,timeTv,songTv;
        View audioview;

        public MusicViewHolder(@NonNull View itemView) {
            super(itemView);
            idTv = itemView.findViewById(R.id.item_local_music_num);
            songTv = itemView.findViewById(R.id.item_local_music_song);
            singerTv = itemView.findViewById(R.id.item_local_music_singer);
            albumTv = itemView.findViewById(R.id.item_local_music_album);
            timeTv = itemView.findViewById(R.id.item_local_music_durtion);
            audioview = itemView.findViewById(R.id.audioview);
        }
    }
}
